package 上课案例.IODemowuxingyi.src.main.java.org.iosf.dao;

import java.io.*;

import 上课案例.IODemowuxingyi.src.main.java.org.iosf.pojo.Goods;
public class GoodsDAO {
    String fileName = "goodsInfo.txt";
    File file;

    public GoodsDAO() {
        File file = new File(fileName);
        if (!file.exists()) {
            try {
                file.createNewFile();
                System.out.println("文件初始化成功");
            } catch (Exception e) {

                throw new RuntimeException();
            }
        }
    }

    public void addGoods(Goods goods) {
        System.out.println(goods);
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            ObjectOutputStream outputStream = new ObjectOutputStream(fileOutputStream);
//            outputStream.write(goods);
            outputStream.writeUTF("\n");
            outputStream.flush();

            outputStream.flush();
        } catch (Exception e) {
            throw new RuntimeException();
        }
        System.out.println("商品成功导入");
    }

    public void updateGoods(Goods goods) {

        try {
            FileInputStream fileOutputStream = new FileInputStream(file);
//            ObjectOutputStream outputStream=new ObjectOutputStream(fileOutputStream);

//            outputStream.flush();
        } catch (Exception e) {
            throw new RuntimeException();
        }
        System.out.println("商品入库成功");

    }

    public void deleteGoods(String goodsId) {

    }

    public void findAllGoods(String goodsId) {

    }

    public void findGoodsByID(String goodsID)  {
//        try {
//            FileInputStream fileInputStream=new FileInputStream(file);
//            ObjectInputStream inputStream=new ObjectOutputStream(fileInputStream);
//            Object o=inputStream.readObject();
//            while (o!=null){
//                o=inputStream.readObject();
//            }
//        }catch ( Exception e){
//            throw new RuntimeException();
//        }
        }
    }
